package com.rockwell.sniffyhunter.utils;

import java.util.Locale;

import android.content.Context;
import android.speech.tts.TextToSpeech;
import android.util.Log;

public class Speaker implements TextToSpeech.OnInitListener {
	private TextToSpeech m_tts;
	private boolean m_canSpeak = false;
	private boolean m_inited = false;
	
	public Speaker(Context context) {
		m_tts = new TextToSpeech(context, this);
	}

	@Override
	public void onInit(int status) {
		m_inited = true;
		
		if (status == TextToSpeech.SUCCESS) {

			int result = m_tts.setLanguage(Locale.US);

			if (result == TextToSpeech.LANG_MISSING_DATA
					|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
				Log.e("TTS", "This Language is not supported");
			} else {
				result = m_tts.setSpeechRate(1.0f);
				m_canSpeak = true;
				speak("Sniffy Hunter is ready");
			}

		} else {
			Log.e("TTS", "Initilization Failed!");
		}
	}
	
	public void shutDown() {
		if (m_tts != null) {
			m_tts.stop();
			m_tts.shutdown();
		}
	}

	public boolean canSpeak() {
		return m_canSpeak;
	}
	
	public boolean hasInitialized() {
		return m_inited;
	}
	
	public void speak(String sentence) {
		if (m_canSpeak) {
			m_tts.speak(sentence, TextToSpeech.QUEUE_FLUSH, null);
		}
	}
}
